//== GAIA ============================================================================

//== BEGINNING OF CODE ===============================================================

#include "sagittarius/core/SagittariusCore.h"

#include "sagittarius/unicode/CUnicode.h"
#include "sagittarius/lexer/lParser.h"

#include <stdio.h>
#include <string.h>
#include <malloc.h>

int main(int argc, char* argv[])
{
    const char* buf = "identifier = ab";
    size_t data_size = strlen(buf);
    printf("length of input stream: %lu\n", data_size);
    
    //create buffer for unicode values
    UnicodeCChar* ubuf = (UnicodeCChar*)malloc((data_size+1)*sizeof(UnicodeCChar)); //size+1 is important for storing the null-terminating value
    assert(ubuf && "Unable to allocate memory");
    
    //convert
    int result = UTF8_ConvertBuffer(buf, ubuf);
    if(result < 0)
    {
        fprintf(stderr, "Failed to convert buffer, routine returned with code %d\n", result);
        free(ubuf);
        return -1;
    }
    
    printf("Unicode buffer:\n");
    printUnicodeBuf(ubuf);
    printf("\n\n");
    
    //Do parsing
    lparserState ps = lex_createParserState(ubuf);
    lparserInfo pi;
    pi.emptychr = 0x1D700;
    lex_parseWS(&ps, &pi);
    lASTNode* a = lex_parseAssn(&ps, &pi);
    //output
    if(a) {
        printf("--Parsed AST:--\n");
        lex_writeOut(2, a, 0);
        lAST_destroy_node(a);
    } else {
        printf("Failed to parse assignment statement\n");
    }
    
    //free buffer
    free(ubuf);
    
    return 0;
}
